package com.yiguantong.police.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiguantong.police.R;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 网络加载中dialog
 * @Author qiuxiaolong 2017/05/06
 */
public class LoadingDialog extends Dialog {

	/*文本录入*/
	private TextView						mTextView									= null;
	/*文本*/
	private String							mText										= null;
    /*动画图片*/
    private ImageView                      mAnimImageView	                  			= null;
    /*动画效果图*/
    private AnimationDrawable              mAnim				            			= null;

	/**
	 * 系统回调方法，初始化dialog
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_nobutton_main);

		this.getWindow().setGravity(Gravity.CENTER);
		LayoutParams lp = this.getWindow().getAttributes();
		lp.alpha = 1f;
		this.getWindow().setAttributes(lp);
		findViewById(R.id.rootView).setAlpha(1f);

		mAnimImageView = (ImageView) findViewById(R.id.dialog_waiting_anim);
		mAnim = (AnimationDrawable) mAnimImageView.getBackground();

		mTextView = (TextView) findViewById(R.id.dialog_nobutton_text);
		mAnim.start();
	}

	/**
	 * 构造方法，可以传入theme的id
	 *
	 * @param context
	 * @param theme
	 */
	public LoadingDialog(Context context, int theme) {
		super(context, theme);
	}

	/**
	 * 构造方法
	 *
	 * @param context
	 */
	public LoadingDialog(Context context) {
		super(context, R.style.CommonLibWaitDialog);
	}


	/**
	 * 设置dialog上文本显示内容
	 *
	 * @param mText
	 */
	public void setmText(String mText) {
		this.mText = mText;
		if (mText!=null) {
			mTextView.setText(mText);
		}
	}

	/**
	 * 设置dialog自动消失
	 *
	 * @param dialog
	 * @param time
	 */
	public void setDismiss(final LoadingDialog dialog, int time) {
		final Timer timer = new Timer();
		timer.schedule(new TimerTask() {

			@Override
			public void run() {
				mAnim.stop();
				dialog.dismiss();
			}
		}, 2000);
	}

	/**
	 * Activity不变暗处理
	 *
	 * @param isHightlight
	 */
	public void setActivityHighlight(boolean isHightlight) {
		if (isHightlight) {
			Window mWindow = getWindow();
			LayoutParams lp = mWindow.getAttributes();
			lp.dimAmount = 0f;
			mWindow.setAttributes(lp);
		}
	}
}
